package com.hwh.daoIm;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import com.hwh.dao.SellersDao;
import com.hwh.vo.Buyers;
import com.hwh.vo.Sellers;

import JDBCUtil.UTIL;

public class SellersDaoImp implements SellersDao {

	// 查询全部卖家用户
	public ArrayList<Sellers> getSellers() {

		ArrayList<Sellers> sellers = new ArrayList<Sellers>();
		PreparedStatement pst = null;
		Connection con = UTIL.getConn();

		ResultSet rs = null;
		StringBuffer sql = new StringBuffer("select * from sellers");
		try {
			pst = con.prepareStatement(sql.toString());
			// 返回一个结果集
			rs = pst.executeQuery();
			while (rs.next()) {
				// 商品对象接收结果集的结果
				Sellers sell = new Sellers();
				sell.setSell_id(rs.getInt("sell_id"));
				sell.setSell_name(rs.getString("sell_name"));
				sell.setSell_pwd(rs.getString("sell_pwd"));
				sell.setSell_phone(rs.getInt("sell_phone"));

				sellers.add(sell);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, pst, con);
			} catch (SQLException e) {

				e.printStackTrace();
			}
		}

		return sellers;
	}

	// 卖家用户登录
	public Sellers buyerLogin(Sellers seller) {

		Sellers sellers = new Sellers();
		StringBuffer sql = new StringBuffer("select * from sellers where sell_name=? and sell_pwd=?");
		Connection conn = UTIL.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, seller.getSell_name());
			ps.setString(2, seller.getSell_pwd());
			rs = ps.executeQuery();
			if (rs.next()) {
				sellers = new Sellers(rs.getInt("sell_id"), rs.getString("sell_name"), rs.getString("sell_pwd"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, ps, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

		return sellers;

	}

	// 插入卖家用户数据
	public void sellerRegist(Sellers seller) {

		StringBuffer sql = new StringBuffer("insert into sellers values('',?,?,?)");
		Connection conn = UTIL.getConn();
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql.toString());
			ps.setString(1, seller.getSell_name());
			ps.setString(2, seller.getSell_pwd());
			ps.setInt(3, seller.getSell_phone());
			ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			try {
				UTIL.closeConn(rs, ps, conn);
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}

}
